//
//  InspListDetailConfirmVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/18.
//
// 【首页-维保服务】巡检明细 - 确认巡检

import Foundation
import UIKit

class InspListDetailConfirmVC: SubLevelViewController {
    let detailV = InspListDetailConfirmView()
    
    var modal: ParListModal!
    
    var param = ParParam()

    init(withModal _modal: ParListModal) {
        super.init(nibName: nil, bundle: nil)
        self.modal = _modal
        self.param.id = self.modal.formId
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "巡检明细"
        view.backgroundColor = .bg
        setupUI()
        reloadData()
    }
    
    func reloadData() {
        detailV.updateUI(withModal: modal)

        view.showToastActivity()
        API.getEngineerDetail(withParam: EngineerParam(formId: modal.formId)) { responseModel in
            DispatchQueue.main.async {
                self.detailV.updateUI(withModal: responseModel.model)
                self.view.hideToastActivity()
            }
        }
    }
    
    func submitTapped() {
        // 网点工作人员确认
        // 确认人签名：bxqm id：是接口中的formId
        // 网点工作人员确认
        view.showToastActivity()
        let path = FilesManager.shared.saveImage(img: detailV.signV.img!)
        API.postYjwxUpload(withImage: path!) { responseModel in
            self.param.bxqm = responseModel.model?.imgUrl
            self.didConfirm()
        }
    }
    
    func didConfirm() {
        // 网点工作人员确认
        API.postParBranchStaffConfirm(withParam: param) { responseModel in
            DispatchQueue.main.async {
                self.view.hideToastActivity()
                if responseModel.errorCode == .Success {
                    self.view.showToast(witMsg: responseModel.errorMessage)
                    Utils.delay(second: 1) {
                        NotificationCenter.default.post(name: NotiInsp, object: nil)
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
        }
    }
    
    // 3是 满意，4是一般  7是不满意
    @objc func submitBMYTapped() {
        if detailV.signV.img == nil {
            view.showToast(witMsg: "请签名")
            return
        }
        
        param.status = "7"
        self.popup.dialog(with: false, bgColor: .black.withAlphaComponent(0.65), container: {
            let padding = (ScreenWidth  - 280.0) / 2.0
            let v = ReplyPopView(frame: CGRect(x: padding, y: 24, width: 280, height: ScreenHeight))
            v.titleL.text = "评价留言"
            v.selectTextWith = { (msg) -> () in
                self.param.remark = msg
                self.submitTapped()
            }
            return v
        })
    }
    
    @objc func submitYBTapped() {
        if detailV.signV.img == nil {
            view.showToast(witMsg: "请签名")
            return
        }
        
        param.status = "4"
        param.remark = nil
        submitTapped()
    }
    
    @objc func submitMYTapped() {
        if detailV.signV.img == nil {
            view.showToast(witMsg: "请签名")
            return
        }
        
        param.status = "3"
        param.remark = nil
        submitTapped()
    }
    
    func setupUI() {
        detailV.submitBMYBtn.addTarget(self, action: #selector(submitBMYTapped), for: .touchUpInside)
        detailV.submitYBBtn.addTarget(self, action: #selector(submitYBTapped), for: .touchUpInside)
        detailV.submitMYBtn.addTarget(self, action: #selector(submitMYTapped), for: .touchUpInside)
        view.addSubview(detailV)
        detailV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
